package com.books.controller.order;


import com.books.model.bo.Result;
import com.books.model.dto.BuyOrderDto;
import com.books.model.entity.address.Receiving_address;
import com.books.model.entity.login_regis.UserLogin;
import com.books.model.entity.order.BuyOrder;
import com.books.model.entity.order.OrderList;
import com.books.service.address.impl.AddressServiceImpl;
import com.books.service.order.IBuyOrderServer;
import com.books.service.order.impl.OrderListServerImpl;
import com.books.util.Md5Util;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @GetMapping("/order")
    public String order1(){
        return "order/orders";
    }


    @Autowired
    private BuyOrder buyOrder;
    @Autowired
    private IBuyOrderServer buyOrderServer;
    @Autowired
    private AddressServiceImpl addressService;
    @Autowired
    private Receiving_address receiving_address;
    @Autowired
    private BuyOrderDto buyOrderDto;
    @Autowired
    private OrderListServerImpl orderListServer;
    @Autowired
    private OrderList orderList;
    @Autowired(required = false)
    private Md5Util md5Util;
    @Autowired
    private UserLogin userLogin;


    @GetMapping("/all")
    @ResponseBody
    public JSONArray findAll(Integer peopleId){
        List<BuyOrder> buyOrderList = buyOrderServer.findAllOrder (peopleId);
        if(buyOrderList!=null && buyOrderList.size ()>0){
            List<BuyOrderDto> buyOrderUtils = new ArrayList<> ();
            for (BuyOrder order : buyOrderList) {
                String address = addressService.findById (order.getAddressId ()).getAddress ();
                BuyOrderDto buyOrderUtil = new BuyOrderDto (order.getBuyId (),order.getUserId (),order.getBuyTime (),order.getStateId (),order.getPayId (),address,order.getTotalMoney ());
                buyOrderUtils.add (buyOrderUtil);
            }
            JSONArray jsonArray = JSONArray.fromObject (buyOrderUtils);

            return jsonArray;
        }else{
            return null;
        }

    }

    @GetMapping("/orderList")
    @ResponseBody
    public JSONArray findOrderList(String buyId){
        List<OrderList> orderLists = orderListServer.findAllList (buyId);
        JSONArray jsonArray = JSONArray.fromObject (orderLists);
        return jsonArray;
    }

    @GetMapping("/confirm")
    @ResponseBody
    public Result confirm(HttpServletRequest request,String id, String password){
        HttpSession session =request.getSession ();
        userLogin= (UserLogin) session.getAttribute ("user");
        String pas = userLogin.getPassword ();
        System.out.println (Md5Util.code (password));
        System.out.println (pas);
        if(password == null || password == ""){
            return new Result ("201","密码不能为空");
        }
        if(!Md5Util.code (password).equals (pas)){
            return new Result ("202","密码错误");
        }

            orderListServer.upStId (3,id);
            System.out.println (id);
            return new Result ("200","收货成功");


    }


}
